package com.domino.client.view;

import java.util.Random;

import com.domino.client.mvc.Controller;
import com.domino.client.mvc.View;
import com.domino.client.mvc.ViewUpdateCallback;
import com.domino.client.view.board.BoardGameCallback;
import com.domino.client.view.board.BoardPanel;
import com.domino.client.view.historicboard.HistoricBoard;
import com.domino.shared.Player;
import com.domino.shared.SessionStateResponse;
import com.domino.shared.model.Brick;
import com.domino.shared.model.Point;
import com.domino.shared.model.SimpleBoard;
import com.domino.shared.session.SessionId;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Client controller.
 * Creates all GUI objects.
 *
 */
public class BoardGamePanelDebug extends BoardGamePanel {
	
	private Button autoplay;
	private TextBox autoplayseed;
	private Random autoplayRandom;
	private ListBox debugInfoListBox;
	private Label clientseed;
	
	public static boolean isDebugPage() {
		return RootPanel.get("autoplay")!=null;
	}
	
	public BoardGamePanelDebug(Controller controller, int size) {
		super(controller, size);
		
		addDebugComponents();
	}

	
	private void addDebugComponents() {
		autoplay = new Button("Autoplay");
		autoplay.setStyleName("domino-debug");
		clientseed = new Label("Client seed:");
		autoplayseed = new TextBox();
		long seed = System.currentTimeMillis();
		autoplayseed.setText(Long.toString(seed));
		autoplayseed.setStyleName("domino-debug");
		RootPanel.get("autoplay").add(autoplay);
		RootPanel.get("clientseed").add(clientseed);
		RootPanel.get("autoplayseed").add(autoplayseed);
		
		autoplay.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				long seed = Long.parseLong(autoplayseed.getText());
				autoplayRandom = new Random(seed);
				autoplay.setEnabled(false);
				waitForOpponentResponse();
			}
		});
		
		this.debugInfoListBox = new ListBox();
		debugInfoListBox.setWidth("100");
		debugInfoListBox.setVisibleItemCount(4);
		debugInfoListBox.setStyleName("domino-debug");
		debug("-----------Debug-----------");
		RootPanel.get("debuginfo").add(debugInfoListBox);
		
	}
	
	
	
	
	private void makeAutoStep() {
		int sizep = getBoard().getAvailblePoints(getClientColor()).size();
		if (sizep==0) {
			putBrick(null);
		} else {
			int index = autoplayRandom.nextInt(sizep);
			Point []points = new Point[sizep];
			points = getBoard().getAvailblePoints(getClientColor()).toArray(points);
			Point select = points[index];
			putBrick(select);
		}
	}

	private boolean isAutoPlaying() {
		return autoplayRandom!=null;
	}
	
	@Override
	public void debug(String message) {
		super.debug(message);
		debugInfoListBox.insertItem(message,0);
	}


	@Override
	public boolean notifyMakeAmove() {
		if (isAutoPlaying()) {
			boolean bugDetect = waitForUserInput();
			makeAutoStep();
			return bugDetect;
		} else {
			return super.notifyMakeAmove();
		}
	}


 }
